Completed
Pull Request — master (#50)
by Pieter Epeüs
21:16 queued 03:07
created

objects.js ➔ getFlatKeys   A

Complexity

Conditions 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 4
1
/**
2
 * Object helper
3
 */
4
module.exports = class Obj {
5
    /**
6
     * Set the original and prefix.
7
     *
8
     * @param {object} original
9
     * @param {string} prefix
10
     *
11
     * @return {object}
12
     */
13
    constructor(original, prefix) {
14
        this.original = original;
15
        this.prefix = prefix;
16
        this.flatObject = {};
17
        this.parse();
18
19
        return this;
0 ignored issues
show
Bug introduced by
The constructor does not have a meaningful return value. Are you sure this is correct?
Loading history...
20
    }
21
22
    /**
23
     * flatten the object 1 level per time.
24
     */
25
    parse() {
26
        Object.entries(this.original).forEach(
27
            ([originalRowIndex, originalRow]) => {
28
                let index = originalRowIndex;
29
30
                if (this.prefix) {
31
                    index = [this.prefix, originalRowIndex].join('.');
32
                }
33
34
                if (typeof originalRow === 'object') {
35
                    const childRows = new Obj(originalRow, index).flat;
36
37
                    this.flatObject = Object.assign(this.flatObject, childRows);
38
39
                    return;
40
                }
41
42
                this.flatObject[index] = originalRow;
43
            }
44
        );
45
    }
46
47
    /**
48
     * Get the flat object.
49
     *
50
     * @return {object}
51
     */
52
    get flat() {
53
        return this.flatObject;
54
    }
55
56
    /**
57
     * Get the object entries.
58
     *
59
     * @return {array}
60
     */
61
    entries() {
62
        return Object.entries(this.flatObject);
63
    }
64
65
    /**
66
     * Get the object keys.
67
     *
68
     * @return {array}
69
     */
70
    keys() {
71
        return Object.keys(this.flatObject);
72
    }
73
74
    /**
75
     * Get the object values.
76
     *
77
     * @return {array}
78
     */
79
    values() {
80
        return Object.values(this.flatObject);
81
    }
82
83
    /**
84
     * Get the object length.
85
     *
86
     * @return {number}
87
     */
88
    get length() {
89
        return Object.keys(this.flatObject).length;
90
    }
91
92
    /**
93
     * Get an item by key.
94
     *
95
     * @param {string} key
96
     * @param {object|null} defaultValue
97
     *
98
     * @return {object|null}
99
     */
100
    getByKey(key, defaultValue) {
101
        if (this.originalHas(key)) {
102
            return Object.getOwnPropertyDescriptor(this.original, key).value;
103
        }
104
105
        if (this.has(key)) {
106
            return this.flatObject[key];
107
        }
108
109
        if (this.includes(key)) {
110
            return this.entries()
111
                .filter(([currentKey]) => currentKey.startsWith(key))
112
                .reduce((accumulator, [currentKey, currentValue]) => {
113
                    const subKey = currentKey.substring(key.length + 1);
114
                    accumulator[subKey] = currentValue;
115
                    return accumulator;
116
                }, {});
117
        }
118
119
        return defaultValue;
120
    }
121
122
    /**
123
     * Get keys of an item.
124
     *
125
     * @param {array} keys
126
     * @param {object|null} defaultValue
127
     *
128
     * @return {object|null}
129
     */
130
    getFlatKeys(keys, defaultValue) {
131
        const result = this.entries().filter(([currentKey]) =>
132
            keys.some(key => currentKey.startsWith(key))
133
        );
134
135
        if (result.length < 1) {
136
            return defaultValue;
137
        }
138
139
        return Object.fromEntries(result);
140
    }
141
142
    /**
143
     * Get keys of an item.
144
     *
145
     * @param {array} keys
146
     * @param {object|null} defaultValue
147
     *
148
     * @return {object|null}
149
     */
150
    getKeys(keys, defaultValue) {
151
        const result = keys.reduce((accumulator, currentKey) => {
152
            const key = currentKey.toString();
153
            const value = this.getByKey(key);
154
155
            if (value) {
156
                accumulator[key] = value;
157
            }
158
159
            return accumulator;
160
        }, {});
161
162
        if (Object.keys(result).length < 1) {
163
            return defaultValue;
164
        }
165
166
        return result;
167
    }
168
169
    /**
170
     * Check if the original object has a key.
171
     *
172
     * @param {string} key
173
     *
174
     * @return {boolean}
175
     */
176
    originalHas(key) {
177
        return Object.prototype.hasOwnProperty.call(this.original, key);
178
    }
179
180
    /**
181
     * Check if the object has a key.
182
     *
183
     * @param {string} key
184
     *
185
     * @return {boolean}
186
     */
187
    has(key) {
188
        return Object.prototype.hasOwnProperty.call(this.flatObject, key);
189
    }
190
191
    /**
192
     * Check if the object has a key that includes.
193
     *
194
     * @param {string} key
195
     *
196
     * @return {boolean}
197
     */
198
    includes(key) {
199
        return this.keys().filter(item => item.startsWith(key)).length > 0;
200
    }
201
};
202